package com.whosly.rapid.lang.thread;

import java.util.HashMap;
import java.util.Map;

/**
 * 随 AsyncLoadCallable 传递的异步线程配置
 */
public class AsyncLoadConfig {
    /**
     * 默认不设置超时,保持系统兼容性
     */
    public static final Long DEFAULT_TIME_OUT = 0L;
    private volatile Long defaultTimeout = DEFAULT_TIME_OUT;
    /**
     * 默认不开启,如果启用可以共享ThreadLocal，需慎用
     */
    private volatile Boolean needThreadLocalSupport = false;
    private Map<AsyncLoadMethodMatch, Long> matches;

    public AsyncLoadConfig() {
    }

    public AsyncLoadConfig(Long defaultTimeout) {
        this.defaultTimeout = defaultTimeout;
    }

    public AsyncLoadConfig cloneConfig() {
        AsyncLoadConfig config = new AsyncLoadConfig();
        config.setDefaultTimeout(this.getDefaultTimeout());
        config.setNeedThreadLocalSupport(this.getNeedThreadLocalSupport());
        config.setMatches(this.getMatches());
        return config;
    }

    public Map<AsyncLoadMethodMatch, Long> getMatches() {
        if (matches == null) {
            matches = new HashMap<>(1);
            matches.put(AsyncLoadMethodMatch.TRUE, defaultTimeout);
        }

        return matches;
    }

    public void setMatches(Map<AsyncLoadMethodMatch, Long> matches) {
        this.matches = matches;
    }

    public Long getDefaultTimeout() {
        return defaultTimeout;
    }

    public void setDefaultTimeout(Long defaultTimeout) {
        this.defaultTimeout = defaultTimeout;
    }

    public Boolean getNeedThreadLocalSupport() {
        return needThreadLocalSupport;
    }

    public void setNeedThreadLocalSupport(Boolean needThreadLocalSupport) {
        this.needThreadLocalSupport = needThreadLocalSupport;
    }

}
